package cn.e4j.modular.system.controller;

import cn.e4j.config.properties.E4jProperties;
import cn.e4j.core.base.controller.BaseController;
import cn.e4j.core.cache.CacheKey;
import cn.e4j.core.cache.CacheUtil;
import cn.e4j.core.dto.Dtos;
import cn.e4j.core.dto.impl.Dto;
import cn.e4j.core.dto.requestDtoResolver.DtoParam;
import cn.e4j.core.dto.requestDtoResolver.DtoWapper;
import cn.e4j.core.reqres.response.ErrorResponseData;
import cn.e4j.core.reqres.response.PageReData;
import cn.e4j.core.reqres.response.ResponseData;
import cn.e4j.core.reqres.response.SuccessResponseData;
import cn.e4j.core.shiro.ShiroKit;
import cn.e4j.core.shiro.ShiroUser;
import cn.e4j.core.util.AppCxt;
import cn.e4j.core.util.LangUtils;
import cn.e4j.core.util.RandNum;
import cn.e4j.core.util.SendTelMsgUtils;
import cn.e4j.modular.common.service.impl.SysAuthorityServiceImpl;
import cn.e4j.modular.crm.entity.GwcqT;
import cn.e4j.modular.crm.entity.GwcsT;
import cn.e4j.modular.crm.entity.GwcszT;
import cn.e4j.modular.crm.entity.GwdiT;
import cn.e4j.modular.crm.service.GwamTService;
import cn.e4j.modular.crm.service.impl.*;
import cn.e4j.modular.system.mapper.SysRoleMapper;
import cn.e4j.modular.system.service.SysUserService;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 首页
 *
 * @author wenchuan.li
 * @date 2018/12/05 09:45
 */
@Controller
@RequestMapping("/admin")
@Slf4j
public class AdminController extends BaseController {
    @Autowired
    private GwdhTServiceImpl gwdhTServiceImpl;

    @Autowired
    private GwdbTServiceImpl gwdbTService;

    @Autowired
    private SysAuthorityServiceImpl sysAuthorityService;

    @Autowired
    private GwakTServiceImpl gwakTService;

    @Autowired
    private Gwai700ServiceImpl gwai700Service;

    @Autowired
    private GwcsTServiceImpl gwcsTService;

    @Autowired
    private GwamTService gwamTService;

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private GwdiTServiceImpl gwdiTService;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private E4jProperties e4jProperties;

    /**
     * 跳转到登录页面
     */

    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index() {
        return "/pc/system/admin.html";
    }

    //@RequiresPermissions("add000")
    @RequestMapping(value = "/getData", method = RequestMethod.POST)
    @Cacheable(value = CacheUtil.TEMP, key = "'" + CacheKey.C_ADMIN_GETDATA + "'")
    @ResponseBody
    public ResponseData getData() {

        return SUCCESS_TIP;
    }

    /**
     * 查询产品信息列表
     */
    @RequestMapping("/getNotices")
    @ResponseBody
    public ResponseData getNotices(@DtoParam DtoWapper dtoWapper) {
//        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWDHENT", inDto.getString("ENT"));
        List<Dto> outDto = gwdhTServiceImpl.getNotices(inDto);
        return ResponseData.success(outDto);
    }

    /**
     * 查询产品信息列表
     */
    @RequestMapping("/getGwamList")
    @ResponseBody
    public ResponseData getGwamList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Map<String, Object>> result = gwdbTService.listGwamList(inDto);
        return ResponseData.success(result);
    }

    /**
     * 获取当前用户信息
     */
    @RequestMapping("/getUser")
    @ResponseBody
    public ResponseData getUser(@DtoParam DtoWapper dtoWapper) {
        ShiroUser user = ShiroKit.getUser();
        Dto outDto = Dtos.newDto();
        //outDto.put("user", user);
        if (user.getTYPE().equals("1")) {
            Dto dto = dtoWapper.getDto();
            dto.put("GWAA001", user.getACCOUNT());
            List<Dto> list = gwakTService.getGwaa009(dto);
            if (list.size() > 0) {
                outDto.put("GWAK001", list.get(0).getString("GWAK001"));
                outDto.put("GWAKL003", list.get(0).getString("GWAKL003"));
                outDto.put("GWAK061", list.get(0).getString("GWAK061"));
                outDto.put("GWAK010", list.get(0).getString("GWAK010"));
            }
        } else {
            Dto inDto = dtoWapper.getDto();
            List<Dto> list = sysAuthorityService.getCustomersAuthority(inDto);
            if (list != null) {
                outDto.put("GWAK001", list.get(0).getString("GWAK001"));
                outDto.put("GWAKL003", list.get(0).getString("GWAKL003"));
                outDto.put("GWAK061", list.get(0).getString("GWAK061"));
                outDto.put("GWAK010", list.get(0).getString("GWAK010"));
            } else {
                list = gwakTService.getGwaa009(inDto);
                if (list.size() > 0) {
                    outDto.put("GWAK001", list.get(0).getString("GWAK001"));
                    outDto.put("GWAKL003", list.get(0).getString("GWAKL003"));
                    outDto.put("GWAK061", list.get(0).getString("GWAK061"));
                    outDto.put("GWAK010", list.get(0).getString("GWAK010"));
                }
            }
        }
        return ResponseData.success(outDto);
    }

    /**
     * 获取权限内的客户信息
     */
    @RequestMapping("/getGwakData")
    @ResponseBody
    public ResponseData getGwakData(@DtoParam DtoWapper dtoWapper) {
        Page<Object> page = dtoWapper.getPage();
        Dto inDto = dtoWapper.getDto();
        ShiroUser user = ShiroKit.getUser();
        List<Dto> list = new ArrayList<>();
        if (user.getTYPE().equals("1")) {
            inDto.put("GWAA001", user.getACCOUNT());
            list = gwakTService.getGwaa009(page, inDto);
        } else {
            //list = sysAuthorityService.getCustomersAuthority(inDto);
            inDto.put("CUSTOMERS", sysAuthorityService.getCustomersAuthority(inDto));
            list = gwakTService.getGwaa009(page, inDto);
        }
        return new PageReData(page, list);

    }

    /**
     * 获取权限内的客户信息
     */
    @RequestMapping("/getGwcqData")
    @ResponseBody
    public ResponseData getGwcqData(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> list = gwai700Service.getGwcqData(inDto);
        return ResponseData.success(list);
    }

    /**
     * 查询活动对应赠品信息
     */
    @RequestMapping("/getGwcsz005_006")
    @ResponseBody
    public ResponseData getGwcsz005_006(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWCSENT", shiroUser.getDBC());
        outDto = gwcsTService.getGwcsz005_006(inDto);
        return ResponseData.success(outDto);
    }

    /**
     * 查询产品信息列表
     */
    @RequestMapping("/getGwdc020_021")
    @ResponseBody
    public ResponseData getGwdc020_021(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWCSENT", shiroUser.getDBC());
        outDto = gwcsTService.getGwdc020_021(inDto);
        return ResponseData.success(outDto);
    }

    /**
     * 新增
     */
    @RequestMapping("/add")
    @ResponseBody
    public ResponseData add(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        JSONObject jsonObject = inDto.getReqJsonObject();
        List<GwcsT> gwcsTList = jsonObject.parseArray(jsonObject.getString("list1"), GwcsT.class);
        List<GwcszT> gwcszTList = jsonObject.parseArray(jsonObject.getString("list2"), GwcszT.class);
        gwcsTService.add(gwcsTList, gwcszTList);
        return SUCCESS_TIP;
    }

    /**
     * 查询非组合产品的阶梯价格
     */
    @RequestMapping("/getPriceSingel")
    @ResponseBody
    public ResponseData getPriceSingel(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        List<Dto> result2 = new ArrayList<>();
        Page<Object> page = dtoWapper.getPage();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWCSENT", shiroUser.getDBC());
        inDto.put("lang", LangUtils.getLang(shiroUser.getLANG()));
        Dto result = Dtos.newDto();
        result = gwcsTService.getPriceSingel(inDto);
        return ResponseData.success(result);
    }

    /**
     * 添加数据
     */
    @RequestMapping("/saveData")
    @ResponseBody
    public ResponseData saveData(GwcqT gwcqT) {
        gwai700Service.addData(gwcqT);
        return SUCCESS_TIP;
    }

    /**
     * 删除数据
     */
    @RequestMapping("/deleteGwcqBatch")
    @ResponseBody
    public ResponseData deleteGwcqBatch(GwcqT gwcqT) {
        gwai700Service.deleteGwcqBatch(gwcqT);
        return SUCCESS_TIP;
    }

    /**
     * 删除数据
     */
    @RequestMapping("/deleteGwcq")
    @ResponseBody
    public ResponseData deleteGwcq(GwcqT gwcqT) {
        gwai700Service.deleteGwcq(gwcqT);
        return SUCCESS_TIP;
    }

    @RequestMapping("/getGwdc020_021_jieti")
    @ResponseBody
    public ResponseData getGwdc020_021_jieti(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        ShiroUser shiroUser = ShiroKit.getUser();
        inDto.put("GWCSENT", shiroUser.getDBC());
        outDto = gwcsTService.getGwdc020_021_jieti(inDto);
        return ResponseData.success(outDto);
    }

    /**
     * 查询最新产品信息
     */
    @RequestMapping("/getGwan")
    @ResponseBody
    public ResponseData getGwan(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwamTService.getGwan(inDto);
        return ResponseData.success(result);
    }

    /**
     * 查询最新产品信息
     */
    @RequestMapping("/getGwamDetail")
    @ResponseBody
    public ResponseData getGwamDetail(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        List<Dto> result = gwdbTService.getGwamDetail(inDto);
        return ResponseData.success(result);
    }

    /**
     * 查询未查看消息列表
     */
    @RequestMapping("/getGwdiList")
    @ResponseBody
    public ResponseData getGwdiList(@DtoParam DtoWapper dtoWapper) {
        Dto inDto = dtoWapper.getDto();
        Page<Object> page = dtoWapper.getPage();
        ShiroUser user = ShiroKit.getUser();
        List<Dto> gwam = gwdiTService.getGwamList(inDto);
        if(gwam.size() > 0){
            for(Dto gwamDto : gwam){
                GwdiT gwdiT = new GwdiT();
                gwdiT.setGWDIENT(user.getDBC());
                //int gwdi001 = gwdiTService.getMaxGwal002(inDto);
                //gwdiT.setGWDI001(new BigDecimal(gwdi001));
                gwdiT.setGWDI002("产品资料维护");
                gwdiT.setGWDI003(gwamDto.getString("GWAM001"));
                gwdiT.setGWDI004(LocalDate.now());
                gwdiT.setGWDI005("产品编号:" + gwamDto.getString("GWAM001") + "无效，请审核!");
                gwdiT.setGWDI006("N");
                gwdiT.setGWDI007("N");
                gwdiT.setGWDIACTI("Y");
                gwdiT.insert();
            }
        }
        inDto.put("GWDI006","N");
        List<Map<String, Object>> list = sysRoleMapper.listRolesOfUser(user.getID());
        List<Dto> role = new ArrayList<>();
        for(Map<String, Object> item : list){
            Dto dto = Dtos.newDto();
            if(item.get("NAME").toString().equals("财务部")){
                dto.put("role", "渠道价格");
                role.add(dto);
                Dto pDto = Dtos.newDto();
                pDto.put("role", "组合促销活动");
                role.add(pDto);
                Dto pDto1 = Dtos.newDto();
                pDto1.put("role", "非组合促销活动");
                role.add(pDto1);
                Dto pDto2 = Dtos.newDto();
                pDto2.put("role", "合同协议");
                role.add(pDto2);
                Dto pDto3 = Dtos.newDto();
                pDto3.put("role", "订单变更");
                role.add(pDto3);
            } else if(item.get("NAME").toString().equals("技术部")){
                dto.put("role", "产品资料维护");
                role.add(dto);
            }else if(item.get("NAME").toString().equals("销售部助理")){
                dto.put("role", "注册信息维护");
                role.add(dto);
            } else if(item.get("NAME").toString().equals("销售总监")){
                dto.put("role", "客户资料维护");
                role.add(dto);
            }
        }
        List<Map<String, Object>> outDto = new ArrayList<>();
        if(role.size() > 0){
            inDto.put("role", role);
            outDto = gwdiTService.getGwdiList(page,inDto);
        }
        return ResponseData.success(outDto);
    }

    @RequestMapping("/getSmsCode")
    @ResponseBody
    public ResponseData getSmsCode(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        // controller中随机创建一个randNum
        String randNum = RandNum.createRandNum();
        System.out.println("验证码是：" + randNum);
        // randNum发送给手机号
        String result = SendTelMsgUtils.sendMsgTo(inDto.getString("cellphone"), randNum);
        System.out.println(result);
        outDto.put("smscode_back",randNum);
        return new SuccessResponseData(outDto);

    }
    @RequestMapping("/checkCellPhone")
    @ResponseBody
    public ResponseData checkCellPhone(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        Dto result = sysUserService.checkCellPhone(inDto);
        if(result.size()>0){
            if(result.getBigDecimal("num").intValue()>= new BigDecimal(1).intValue()){
                outDto.put("num",result.getBigDecimal("num"));
            }else {
                outDto.put("num",0);
            }
        }else {
            outDto.put("num",0);
        }
        return new SuccessResponseData(outDto);
    }

    @RequestMapping("/saveNewPassword")
    @ResponseBody
    public ResponseData saveNewPassword(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        Dto inDto = dtoWapper.getDto();
        inDto.put("password", ShiroKit.md5(inDto.getString("password")));
        sysUserService.saveNewPassword(inDto);
        return SUCCESS_TIP;
    }


    @RequestMapping("/getDigit")
    @ResponseBody
    public ResponseData getDigit(@DtoParam DtoWapper dtoWapper) {
        Dto outDto = Dtos.newDto();
        try {
            // 汇率小数位
            outDto.put("rate", Integer.parseInt(AppCxt.getParam("rate_digit_")));
            // 单价小数位
            outDto.put("price", Integer.parseInt(AppCxt.getParam("price_digit_")));
            // 金额小数位
            outDto.put("amount", Integer.parseInt(AppCxt.getParam("amount_digit_")));

        } catch (NumberFormatException e) {
            e.printStackTrace();
            return new ErrorResponseData();
        }
        return new SuccessResponseData(outDto);
    }

}
